var questionId;
$(function () {
    if(sessionStorage.getItem('id')) {
        loadData(sessionStorage.getItem('id'));
        $('.box-title').html('编辑答案')
    }else{
        createPic()
        createVideo()
        $('.box-title').html('新增答案')
    }
    // createAudio()
    $('#submit').on('click',function () {
        submit()
    })
});

function loadData(id){
    ajaxCommon.request('get',baseQuestionUrl + 'findanswer/'+id,{},function(res){
        var data=res.data[0];
        if(data.photo){
            createPic(true);
        }else{
            createPic();
        }
        if(data.video){
            createVideo(true);
        }else{
            createVideo();
        }
        $('#answers').val(data.answer);
        $('#voicetext').val(data.voicetext);
        // 图片展示
        $('.picUrl').val(data.photo).siblings('.prePic').css({
            background: 'url("' + data.photo + '") center no-repeat',
            backgroundSize: 'cover'
        })
        createPic()
        // 视频显示
        if(data.video) {
            $('.videoUrl:eq(0)').val(data.video).siblings('video').attr('src', data.video);
            $('.videoName:eq(0)').val(data.name).show()
            createVideo()
        }
        if(data.video1){
            $('.videoUrl:eq(1)').val(data.video1).siblings('video').attr('src', data.video1);
            $('.videoName:eq(1)').val(data.name2).show()
            createVideo()
        }else if(data.video2){
            $('.videoUrl:eq(2)').val(data.video2).siblings('video').attr('src', data.video2);
            $('.videoName:eq(2)').val(data.name3).show()
            createVideo()
        }
        questionId = data.questionid
        console.log(data,questionId)
    })
}

function submit() {
    if (isNull($("#answers").val())) {
        swal("答案概述不能为空");
        return;
    }
    if (isNull($("#voicetext").val())) {
        swal("语音描述不能为空");
        return;
    }
    if ($(".picUrl").val() == "") {
        swal("请选择图片");
        return;
    }
    // if ($(".videoUrl").length < 2) {
    //     swal("请选择视频");
    //     return;
    // }
    submitQuestion();

}

/**
 * 提交对问题的修改
 * @param fileUrl
 */
function submitQuestion() {
    var params = sessionStorage.getItem('id')
    var data = {
        answer: $('#answers').val(),
        photo: $('.picUrl').val(),
        voicetext: $('#voicetext').val(),
        video: $('.videoUrl:eq(0)').val(),
        video1: $('.videoUrl:eq(1)').val() ? $('.videoUrl:eq(1)').val() : '',
        video2: $('.videoUrl:eq(2)').val() ? $('.videoUrl:eq(2)').val() : '',
        name: $('.videoName:eq(0)').val(),
        name2: $('.videoName:eq(1)').val() ? $('.videoName:eq(1)').val() : '',
        name3: $('.videoName:eq(2)').val() ? $('.videoName:eq(2)').val() : '',
        questionid: questionId ? questionId : ''
    };
    if (isNull(params)) {
        url = baseQuestionUrl + "add/answer";
        data.id = 0;
    } else {
        url = baseQuestionUrl + "update/answer";
        data.id = params;
    }
    ajaxCommon.request('post', url, JSON.stringify(data), function (res) {
        if (res.code === 200) {
            swal({
                title: "操作成功",
                type: "success",
                timer: 3000,
                showConfirmButton: false
            })
            initPage('answer','answer/index.html')
        }
    })
}
